import { queryroles,queryTenant } from '/@/api/system/role';
import { BasicColumn, FormSchema } from '/@/components/Table';
import { checkName } from '/@/utils/index';

export const columns: BasicColumn[] = [
  {
    title: '用户名',
    dataIndex: 'UserName',
  },
  {
    title: '昵称',
    dataIndex: 'Name',
  },
  {
    title: '注册时间',
    dataIndex: 'RegisterTime',
  },
  {
    title: '角色',
    dataIndex: 'RoleName',
    width: 200,
  },
  {
    title: '租户',
    dataIndex: 'TenantName',
    width: 200,
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'UserName',
    label: '用户名',
    component: 'Input',
    rules: [
      {
        required: true,
        validator: async (_rule, value) => checkName(value, `用户名`),
        trigger: ['change', 'blur'],
      },
    ],
  },
  {
    field: 'Password',
    label: '密码',
    component: 'StrengthMeter',
    required: true,
    componentProps: {
      placeholder: '请输入密码',
    },
  },
  {
    field: 'ConfirmPassword',
    label: '确认密码',
    component: 'InputPassword',
    componentProps: {
      placeholder: '请输入确认密码',
    },
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator: (_, value) => {
            if (!value) {
              return Promise.reject('密码不能为空');
            }
            if (value !== values.Password) {
              return Promise.reject('两次输入的密码不一致!');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
  {
    label: '角色',
    field: 'Role',
    component: 'ApiSelect',
    componentProps: {
      api: queryroles,
      labelField: 'Name',
      valueField: 'Name',
      resultField: 'Items',
      params: [1, 1000],
    },

    required: true,
  },
  {
    field: 'Name',
    label: '昵称',
    component: 'Input',
    required: true,
  },
  {
    label: '租户',
    field: 'TenantId',
    component: 'ApiSelect',
    componentProps: {
      api: queryTenant,
      labelField: 'Name',
      valueField: 'Id',
      resultField: 'Items',
      params: [],
    },

    required: true,
  },
];
